<?php
/**
 * admin service file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/09/03
 * Time: 15:20
 */

namespace Modules\Admin\Service;

use HiCommon\Repository\ShopModifyRepository;
use HiCommon\Repository\ShopServerTypeRepository;

class ShopServerTypeServer
{


    /**
     *  获取服务类型列表
     *  hinq 2019 11 04
     *
     **/
    public function get_list(array $arr_search = [])
    {

        try {
            $type = empty( $arr_search['type'] ) ? 1 : $arr_search['type'];
            $arr_option['where'] = ['status' => 1, 'type' => $type, 'pid' => 0];
            $arr_option['field'] = ['id', 'name', 'pid' , 'type'];
            $arr_option['with'] = ['shop_server_type'];
            $arr_option['order'] = ['key'=>'sort','value'=>'asc'];
            $arr_list = ShopServerTypeRepository::get_list((array)$arr_option );

            $arr_list->appends($arr_search);//添加链接参数
            $arr_list = list_page_data($arr_list, $arr_search);
            return hello_success('成功', $arr_list);
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }


    /**
     *  添加服务类型数据
     *  hinq 2019 11 05
     *
     **/
    public function add_one(array $arr_input)
    {
        try {
            if (empty($arr_input['name'])) {
                return hello_error('请输入名称');
            }
            $data['pid'] = empty( $arr_input['pid'] ) ? 0 : $arr_input['pid'];
            $data['name'] = $arr_input['name'];
            $data['type'] = empty( $arr_input['type'] ) ? 1 : $arr_input['type'];
            $count = ShopServerTypeRepository::get_count_by_where((array)$data);
            if ($count > 0) {
                return hello_error('该名称已存在');
            }
            $data['sort'] = $arr_input['sort'];
            $data['status'] = 1;
            $res = ShopServerTypeRepository::add_one((array)$data);
            if (empty($res)) {
                return hello_error('添加失败');
            }
            return hello_success('添加成功');
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }

    }

    /**
     *  编辑获取指定的数据
     *  hinq 2019 11 05
     *
     **/
    public function get_one(int $nId)
    {
        try {
            if (empty($nId)) {
                return hello_error('参数错误');
            }
            $info = ShopServerTypeRepository::get_one((int)$nId)->toArray();
            return $info;
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     *  编辑指定的服务数据
     *  hinq 2019 11 05
     *
     **/
    public function edit_one(array $arr_input)
    {

        try {
            if (empty($arr_input['name'])) {
                return hello_error('请输入名称');
            }
            $data['name'] = $arr_input['name'];
            $data['type'] = $arr_input['type'];
            $info = ShopServerTypeRepository::get_one_by_where((array)$data);
            if (!empty($info->first())) {
                if ($info['id'] != $arr_input['id']) {
                    return hello_error('该名称已存在');
                }
            }
            $data['id'] = $arr_input['id'];
            $res = ShopServerTypeRepository::edit_one((array)$data);
            if (empty($res)) {
                return hello_error('编辑失败');
            }
            return hello_success('编辑成功');
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }


    /**
     *  删除指定数据
     *  hinq  2019 11 05
     *
     **/
    public function delete(int $nId)
    {
        try {
            if (empty($nId)) {
                return hello_error('参数错误');
            }

            $where['pid'] = $nId;
            $count = ShopServerTypeRepository::get_count_by_where((array)$where);
            if ($count > 0) {
                return hello_error('子级还存在数据，不可删除');
            }
            $res = ShopServerTypeRepository::delete_one((int)$nId , (bool) true );
            if (empty($res)) {
                return hello_error('删除失败');
            }
            return hello_success('删除成功');
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }
}